alarms
The AlarmManager is a system service that allows you to schedule and perform actions at specified times or intervals, even if your application is not currently running. It provides a way to schedule your application to execute code at a specific time or repeatedly after a certain interval.
The AlarmManager class provides several methods to set and manage alarms, including:
set()
- Sets a one-time alarm that will trigger the specified PendingIntent at the specified time.setRepeating()
- Sets a repeating alarm that will trigger the specified PendingIntent at a specified interval, starting from the specified time.setExact()
- Sets a one-time alarm that will trigger the specified PendingIntent exactly at the specified time, without any inexactness.setExactAndAllowWhileIdle()
- Similar tosetExact()
, but allows the alarm to trigger even if the device is in low-power idle mode.setInexactRepeating()
- Sets a repeating alarm that will trigger the specified PendingIntent at a specified interval, but with inexact timing to optimize battery usage.
These are some of the commonly used methods of AlarmManager. Each method requires a PendingIntent, which encapsulates the action to be performed when the alarm triggers, such as starting a service, sending a broadcast, or launching an activity.
The AlarmManager supports different types of alarms, including:
-
RTC_WAKEUP - This alarm type uses the real-time clock (RTC) and wakes up the device to trigger the alarm. It ignores any sleep or low-power states of the device. Useful for time-sensitive alarms that need to wake up the device from sleep.
-
RTC - This alarm type uses the real-time clock (RTC) but does not wake up the device. The alarm will be delivered to the application when the device is next awake. Useful for alarms that don't require immediate execution.
-
ELAPSED_REALTIME_WAKEUP - This alarm type uses the elapsed real-time clock and wakes up the device to trigger the alarm. It does not use the wall clock time and ignores sleep or low-power states. Useful for time-sensitive alarms that need to wake up the device from sleep.
-
ELAPSED_REALTIME - This alarm type uses the elapsed real-time clock and does not wake up the device. The alarm will be delivered to the application when the device is next awake. Useful for alarms that don't require immediate execution.
When setting an alarm, you need to specify the alarm type and provide the time or interval at which the alarm should trigger. The AlarmManager takes care of managing the alarms even if the device is rebooted, ensuring that the scheduled actions are still triggered as intended.
Example: Here's a simple example of how you can implement an alarm in Android Studio using Java:
First, you'll need to create an XML layout file for the alarm activity. Create a new file called activity_alarm.xml
in the res/layout
directory and add the following code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<Button
android:id="@+id/btn_set_alarm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set Alarm" />
</LinearLayout>
Next, create a new Java class called AlarmActivity
. Replace the existing code in the class with the following code:
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Calendar;
public class AlarmActivity extends AppCompatActivity {
private Button btnSetAlarm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm);
btnSetAlarm = findViewById(R.id.btn_set_alarm);
btnSetAlarm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setAlarm();
}
});
}
private void setAlarm() {
// Set the alarm to start at a specific time
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 8); // Change the hour and minute values as needed
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
// Create an explicit intent for the AlarmReceiver class
Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
// Get the AlarmManager service and set the alarm
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
}
In this code, we set the alarm to trigger at 8:00 AM. You can modify the hour and minute values in the setAlarm()
method as per your requirement.
Next, create another Java class called AlarmReceiver
. This class will handle the alarm event when it is triggered. Replace the existing code in the class with the following code:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Display a toast message when the alarm triggers
Toast.makeText(context, "Alarm triggered!", Toast.LENGTH_SHORT).show();
}
}
Finally, update the AndroidManifest.xml file to include the necessary permissions and declarations. Add the following lines inside the <manifest>
tag:
<uses-permission android:name="android.permission.SET_ALARM" />
<application ...>
...
<receiver android:name=".AlarmReceiver" />
</application>
That's it! You've implemented a basic alarm functionality in Android Studio using Java. When you run the app and click the "Set Alarm" button, it will schedule an alarm for the specified time. When the alarm triggers, a toast message saying "Alarm triggered!" will be displayed.